package com.tiyo.common.utils.crypto;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import com.tiyo.common.exception.BaseException;




/**
 * 
 * @Description AES加密工具类
 * @author yangb
 * @version v1.0
 * @date 2020年5月21日
*
 */
public class AesUtil {
	
	private static final String KEY = "lyy4pGK2Iw0XtQPq";//abcdefgabcdefg12
	private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
	public static String base64Encode(byte[] bytes){
		Encoder encoder = Base64.getEncoder();
		return encoder.encodeToString(bytes);
	}
	public static byte[] base64Decode(String base64Code) throws Exception{
		Decoder decoder = Base64.getDecoder();
		return decoder.decode(base64Code);
	}
	
	public static byte[] encryptToBytes(String content, byte[] key) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
		return cipher.doFinal(content.getBytes("utf-8"));  
	}
	
	/**
	 * Aes加密
	 * @param content
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String content, String key) throws Exception {  
	    return base64Encode(encryptToBytes(content, key.getBytes()));  
	} 
	/**
	 * 默认签名加密
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String content) {  
		try {
			return base64Encode(encryptToBytes(content, KEY.getBytes()));  
		} catch (Exception e) {
			throw new BaseException("加密失败");
		}
	} 
	/**
	 * Aes加密
	 * @param content
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String content,byte[] key) throws Exception {  
	    return base64Encode(encryptToBytes(content, key));  
	}
	/**
	 * aes加密
	 * @param encryptBytes
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String decryptByBytes(byte[] encryptBytes, byte[] key) throws Exception {  
	    KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	    kgen.init(128);  
	    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);  
    	cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));  
	    byte[] decryptBytes = cipher.doFinal(encryptBytes);  
	    return new String(decryptBytes);  
	}  
	
	/**
	 * aes解密
	 * @param encryptStr
	 * @param decryptKey
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String encryptStr, String decryptKey) throws Exception {  
		encryptStr = encryptStr.replace(' ', '+');
	    return decryptByBytes(base64Decode(encryptStr), decryptKey.getBytes());  
	}
	/**
	 * 默认签名解密
	 * @param encryptStr
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String encryptStr) throws Exception {  
		try {
			encryptStr = encryptStr.replace(' ', '+');
			return decryptByBytes(base64Decode(encryptStr), KEY.getBytes());  
		} catch (Exception e) {
			throw new BaseException(e.getMessage());
		}
	}
	/**
	 * aes解密
	 * @param encryptStr
	 * @param decryptKey
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String encryptStr, byte[] decryptKey) throws Exception {  
		encryptStr = encryptStr.replace(' ', '+');
		return decryptByBytes(base64Decode(encryptStr),decryptKey);  
	}  
	
	public static void main(String[] args) {
		String user1 = "SERVICE_TYPE=appLogin&EMPLOYEENO=20130573027&PASSWORD=888888&MAC=9E967F69AA63F871D07A250C544BEF89&MYNAME=杨吉程";
		
//		String user2 = "OkM22YXwBBxiklK+VvgPCtoS03iyl+vNwCMUiB0LjmtTMzxouZa6VmoW5cqGZpKJ/4yPGgHZGuX/EaFFCR2Zsc6VB7TaKIUOBPJzhpicpOc7eFSE0dpnvZp1VqTn9qZkA9YzWhWkmXU6QFlShrFM93SbWtiur7SsD3nxHp1gXCQ=";
//		String ss = "12c1d539c5db238a9390dc96d368b0f3c31cac50833ac9676edb790a841ba9bbcb39c2578abd5d767113e1cdf5c18a4bb21eeb0902743dacc152d58ed05bf4a612d40d37a71b8e57aed92c2fcf531f410ae2e4c4c8613200124c65b82ec3df0edad385c55e3d493e70e3c1eeb8ee076cb0205d02c153fcf0f436772bd6bec82a7532a9edf42c6318c967a77822bcee21883a4f4edc16a560b8c9575cc9add17e";
		try {
			System.out.println();
			System.out.println(user1);
			String enStr=AesUtil.encrypt(user1, KEY);
			System.out.println(enStr);
			System.out.println(AesUtil.decrypt(enStr, KEY));
//			System.out.println(AES_ECB_PKCS5Padding.aesDecrypt(ss, KEY));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(DESede_CBC_PKCS5Padding.decrypt(user2, keys));
	}
}
